/* Copyright 2001,2002,2003 NAH6
 * All Rights Reserved
 *
 * Parts Copyright DoD, Parts Copyright Starium
 *
 */
#include "resample.h"

/*
 *  The comment below is based on the original routine.  This file
 *  implements Resample_short which is the Resample routine with
 *  some params modified and/or eliminated.
 */

/*************************************************************************
*
* ROUTINE
*               Resample
*
* FUNCTION
*
*               Upsample and select appropriate samples to resample
*               at fractional delay
*
* SYNOPSIS
*
*               Resample(sig_in, wsinc, q_frac_pit, out_len,
*                       out_start, int_pit, m1, m2, sig_out)
*
*
*
*   formal
*                       data    I/O
*       name            type    type    function
*       -------------------------------------------------------------------
*       sig_in          fxpt_16  i      Input signal
*       wsinc           fxpt_16  i      Hamming windowed sinc interpolating
*                                       function
*       q_frac_pit      int      i      Quantized fractional pitch
*       out_len         int      i      Length of output signal
*       out_start       int      i      Beginning of output signal
*       int_pit         int      i      Integer pitch
*       m1              int      i      Lower interpolation bound
*       m2              int      i      Upper interpolation bound
*       sig_out         fxpt_16  o      Delayed input signal
**************************************************************************/


void Resample_short(
fxpt_16 sig_in[],                               /* 15.0 format */
fxpt_16 wsinc[MAX_M1+MAX_M2+1][MAX_NFRAC],      /* 0.15 format */
int     q_frac_pit,
int     int_pit,
fxpt_16 sig_out[])                              /* 15.0 format */
{
  int   i;
  fxpt_16 *sip;
  int64 acc;
  fxpt_16 ws0, ws1, ws2, ws3, ws4, ws5, ws6, ws7;

  fxpt_16 si0, si1, si2, si3, si4, si5, si6, si7;

  ws0 = wsinc[0][q_frac_pit];
  ws1 = wsinc[1][q_frac_pit];
  ws2 = wsinc[2][q_frac_pit];
  ws3 = wsinc[3][q_frac_pit];
  ws4 = wsinc[4][q_frac_pit];
  ws5 = wsinc[5][q_frac_pit];
  ws6 = wsinc[6][q_frac_pit];
  ws7 = wsinc[7][q_frac_pit];


  sip = &sig_in[-int_pit - 4];

  si0 = sip[0];
  si1 = sip[1];
  si2 = sip[2];
  si3 = sip[3];
  si4 = sip[4];
  si5 = sip[5];
  si6 = sip[6];

  for (i = 0; i < SF_LEN; i++) {

    si7 = sip[i + 7];

    acc  = (fxpt_32)si0 * ws0;
    acc += (fxpt_32)si1 * ws1;
    acc += (fxpt_32)si2 * ws2;
    acc += (fxpt_32)si3 * ws3;
    acc += (fxpt_32)si4 * ws4;
    acc += (fxpt_32)si5 * ws5;
    acc += (fxpt_32)si6 * ws6;
    acc += (fxpt_32)si7 * ws7;

    si0 = si1;
    si1 = si2;
    si2 = si3;
    si3 = si4;
    si4 = si5;
    si5 = si6;
    si6 = si7;

    //sig_in[i] = acc >> 15;
    sig_in[i] = fxpt_saturate16(fxpt_saturate32_round(acc,15));
  }

#if 1
  for (i = 0; i < SF_LEN; i++)  {
    sig_out[i] = sig_in[i];
    sig_in[i] = 0;
  }
#endif
}
